package com.miplus.ccrm.uploadjob.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.uploadjob.mapper.IouBillPaydetailMapper;
import com.miplus.ccrm.uploadjob.pojo.IouBillPaydetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.ExecutionException;

@Slf4j
@Component
public class JRbtConsumer {

    @Resource
    IouBillPaydetailMapper billPaydetailMapper;

    @RabbitListener(id="MonCCRM",bindings = @QueueBinding(value=@Queue(value="PLR-ENTER-URGERECEIVE.ENTRUST",durable = "true",autoDelete = "false",exclusive = "false"),
            exchange = @Exchange(value= "CCRM-BUSS-EXCHANGE",durable = "true",autoDelete = "false",type ="fanout",ignoreDeclarationExceptions = "true")))
    public void OnReceived(Message message, Channel channel)
    {
        long tag=message.getMessageProperties().getDeliveryTag();

        log.info("Rbt.OnReceived:Channel={},DeliveryTag={}",channel,tag);
        try {
            OnHandleMessage(message);

            log.info("Rbt.OnReceived:Channel={},DeliveryTag={} Done",channel,tag);
        }
        catch (Exception e){
            log.warn("Rbt.OnReceived.Err:{}",e.getMessage());
        }
    }

    private void OnHandleMessage(Message message) throws Exception {

        String rabBody = new String(message.getBody(),"UTF-8");

        log.info("Rbt.OnReceived:Message={}",rabBody);

        JSONObject root=JSON.parseObject(rabBody);
        JSONArray arr=root.getJSONArray("msg");

        log.info("Rbt.OnReceived:MessageCnt={}",arr.size());

        for(int i=0;i<arr.size();i++){
            JSONObject o=arr.getJSONObject(i);
            SaveToBill(o);
        }
    }

    private int SaveToBill(JSONObject o)
    {
        try {
            String seqno=o.getString("repaymentNo");
            IouBillPaydetail billPaydetail = billPaydetailMapper.selectOne(
                    new MPJLambdaWrapper<IouBillPaydetail>()
                            .eq(IouBillPaydetail::getSerialNo,seqno)
            );
            if(billPaydetail!=null){
                log.info("Rbt.SaveToBill({}) has exists",seqno);
                return 0;
            }
            billPaydetail=new IouBillPaydetail();
            billPaydetail.setSerialNo(o.getString("repaymentNo"));
            billPaydetail.setBillid(o.getString("loanNo"));
            billPaydetail.setTradeTime(JPubUtils.StringToDate(o.getString("applicationTime")));
            billPaydetail.setCrdt(new Date());
            billPaydetail.setRepayAmt(o.getBigDecimal("totalAmt"));
            billPaydetail.setRepayPrin(o.getBigDecimal("principalAmt"));
            billPaydetail.setRepayInt(o.getBigDecimal("interestAmt"));
            billPaydetail.setRepayIntOver(o.getBigDecimal("overdueInterestAmt"));
            billPaydetail.setRepayOverdueFee(o.getBigDecimal("overdueFineInterestAmt"));
            billPaydetail.setRepayFee(o.getBigDecimal("poundageAmt"));
            billPaydetail.setRepayCardno(o.getString("repaymentCardNo"));
            billPaydetail.setRepayBank(o.getString("repaymentBankCode"));
            billPaydetail.setRepayOperatorTpye(o.getString("repaymentMode"));
            billPaydetail.setStatus("1");
            return billPaydetailMapper.insert(billPaydetail);
        }
        catch (Exception e){
            log.warn("SaveToBill.Err:{}",e.getMessage());
            return -1;
        }
    }
}
